package sit.engreal.leetcode.util;


public class ListNode {
    public int val;
    public ListNode next;

    public ListNode() {
    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }

    public static boolean listEqual(ListNode p, ListNode q) {
        while (p != null && q != null) {
            if (p.val != q.val) {
                return false;
            }
            p = p.next;
            q = q.next;
        }
        return p == null && q == null;
    }

    public static ListNode array2list(int[] array) {
        ListNode head = new ListNode();
        ListNode p = head;
        for (int i : array) {
            p.next = new ListNode(i);
            p = p.next;
        }
        return head.next;
    }

    public static int[] list2array(ListNode head) {
        int n = 0;
        for (ListNode p = head; p != null; p = p.next) {
            n++;
        }
        int[] array = new int[n];
        n = 0;
        for (ListNode p = head; p != null; p = p.next) {
            array[n] = p.val;
            n++;
        }
        return array;
    }
}
